<?
set_time_limit(100);
require_once("../config/dbconn.php");require_once("../config/powercls.php");

if($act=='tjnetedit'){
	$msg='';
	if(trim($username)=='') $msg="请输入移动的会员编号\\n";
	if(trim($tjuser)=='') $msg="请输入新推荐人编号\\n";
	if (trim($username)==trim($tjuser))  $msg="移动会员和新推荐人编号不能相同\\n";
	if ($msg!=''){
		echo "<script>alert('$msg');history.back();</script>";exit();
	}
	//用户名是否存在
	$sqlchk="select id,tjuser,confirmtime,regpv,tjdepth from {$db_prefix}users where username='".trim($username)."'";
	$rschk=$db->get_one($sqlchk);
	if(!$rschk['id']){
		$msg.="移动的会员编号不存在\\n";
	}
	if ($msg!=''){
		echo "<script>alert('$msg');history.back();</script>";exit();
	}
	$oldtjdepth=$rschk['tjdepth'];$yduserid=$rschk['id'];$oldtjuser=$rschk['tjuser'];
	//新的推荐会员是否存在
	$sqlchk1="select id,username,tjdepth from {$db_prefix}users where username='".trim($tjuser)."'";
	$rschk1=$db->get_one($sqlchk1);
	if(!$rschk1['id']){
		$msg.="新推荐人编号不存在\\n";
	}
	if ($msg!=''){
		echo "<script>alert('$msg');history.back();</script>";exit();
	}
	$newtjdepth=$rschk1['tjdepth']+1;
	//新位置是否允许放置
	$sqlgx1="select tjstr from {$db_prefix}tjgx where userid='".$rschk1['id']."'";
	$rsgx1=$db->get_one($sqlgx1);
	if ($rsgx1['tjstr']){
		$tjnetary1=explode(',',$rsgx1['tjstr']);
		if (in_array($rschk['id'],$tjnetary1)){
			$msg.="新位置不允许放置\\n";
		}
	}
	if ($msg!=''){
		echo "<script>alert('$msg');history.back();</script>";exit();
	}
	//原来的推荐网体获取
	$sqlgx="select tjstr from {$db_prefix}tjgx where userid='".$rschk['id']."'";
	$rsgx=$db->get_one($sqlgx);
	$oldtjstr=$rsgx['tjstr'];
	
	//新推荐的会员的推荐关系
	$newtjstr='';
	if ($rsgx1['tjstr']) $newtjstr=$rsgx1['tjstr'].",".$rschk1['id'];else $newtjstr=$rschk1['id'];
	//更新会员的推荐关系
	$sqlup="update {$db_prefix}users set tjuser='".trim($tjuser)."',tjdepth='$newtjdepth' where username='".trim($username)."'";
	$db->query($sqlup);
	$sqlup1="update {$db_prefix}tjgx set tjstr='".trim($newtjstr)."' where userid='".$rschk['id']."'";
	$db->query($sqlup1);
	
	//插入到修改记录中
	unset($dataArray);
	$dataArray['username']=trim($username);
	$dataArray['tjuser']=trim($tjuser);
	$dataArray['oldtjuser']=$rschk['tjuser'];
	$dataArray['addtime']=$curtime;
	$db->insert("{$db_prefix}tjnetedit",$dataArray);
	
	//更新下级会员的tjstr和tjdepth
	
	$xtjdepth=$newtjdepth-$oldtjdepth;
	$sqlxj="select * from {$db_prefix}tjgx where find_in_set('{$rschk['id']}',tjstr)>0";
	$resultxj=$db->query($sqlxj);
	while($rsxj=$db->fetch_array($resultxj)){
		$tjarytmp=explode(",{$rschk['id']},",$rsxj['tjstr']);
		$newtjstr1='';
		if ($rsgx1['tjstr']) $newtjstr1=$rsgx1['tjstr'].",{$rschk['id']},".$tjarytmp[1];else $newtjstr1="{$rschk['id']},".$tjarytmp[1];
		$sqlup="update {$db_prefix}users set tjdepth=tjdepth+'$xtjdepth' where id='".$rsxj['userid']."'";
		$db->query($sqlup);
		$sqlup1="update {$db_prefix}tjgx set tjstr='".trim($newtjstr1)."' where userid='".$rsxj['userid']."'";
		$db->query($sqlup1);
	}
	$db->free_result($resultxj);
	
	
	//插入到管理员日志中
	$memo="{$username}从{$oldtjuser}移动会员到{$tjuser}";
	setmgrlogjl($_SESSION["sys_adminid"],3,$memo,$curtime);
	
	echo "<script>alert('推荐网体修改成功');location.href='nets_tjxg.php';</script>";exit();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript" src="js/nets_tjxg.js"></script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	
}
.red{
	color:#FF0000
}
-->
</style>

<link href="images/skin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td width="935" height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt">推荐网体修改</div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top">
		<form id="form1" name="form1" method="post" action="?act=tjnetedit">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td colspan="3"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
                <tr>
                  <td class="left_bt2">&nbsp;&nbsp;&nbsp;&nbsp;</td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td height="30" colspan="3">
			  
			  <table width="100%" height="60" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="30%" height="30" align="right" bgcolor="#f2f2f2" class="left_txt">移动的会员编号：</td>
                  <td width="70%" height="30" bgcolor="#f2f2f2" class="left_txt">
                    <input name="username" type="text" id="username" onBlur="usernamefocus()">
                 <label class="red"> *</label>  <label id="username_notice" class="red"></label></td>
                </tr>
                <tr>
                  <td height="30" align="right" class="left_txt">新推荐人编号：</td>
                  <td height="30" class="left_txt">
                    <input name="tjuser" type="text" id="tjuser" onBlur="tjuserfocus()">
                  <label class="red">*</label>  <label id="tjuser_notice" class="red"></label></td>
                </tr>
              </table></td>
            </tr>
            
            <tr>
              <td height="30" colspan="3">&nbsp;</td>
            </tr>
            <tr>
              <td width="50%" height="30" align="right"><input type="submit" value="确定修改" name="B1" /></td>
              <td width="6%" height="30" align="right">&nbsp;</td>
              <td width="44%" height="30">&nbsp;</td>
            </tr>
            <tr>
              <td height="30" colspan="3">&nbsp;</td>
            </tr>
          </table>
		  </form>
		  </td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>

</body>
</html>